package com.example.function_interface.predicate;

import com.example.function_interface.User;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.function.LongPredicate;
import java.util.function.Predicate;

/**
 * 接受一个输入参数，返回一个布尔值结果。
 */
public class PredicateTest {
    @Test
    public void myTest() throws Exception {
        List<User> userList = new ArrayList<>();
        User user1 = new User(1, "张三", 12, false, 23.8);
        User user2 = new User(2, "李四", 22, false, 200);
        User user3 = new User(3, "王五", 32, true, 123.2);
        User user4 = new User(4, "郑静", 2, true, 0);
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);

        Predicate<User> predicate = new Predicate() {
            @Override
            public boolean test(Object o) {
                User user = (User) o;
                return user.getAge() > 18;
            }
        };

        Predicate predicate1 = predicate.and(value -> value.getAge() < 30);
        Predicate predicate2 = predicate.or(value -> value.getAge() < 14);

        System.out.println("18 < age < 30：" + predicate1.test(user1));
        System.out.println("14 > age or age > 18：" + predicate2.test(user1));

        System.out.println("18 > age or age > 30：" + predicate1.negate().test(user1));
        System.out.println("14 < age < 18：" + predicate2.negate().test(user1));
    }
}
